<?php
namespace app\wechat\controller;

use think\Controller;
use think\Session;
use Des3\DES3;
use Wechat\TPWechat;

class Base extends Controller{
    
    public function send_note()
    {
        $param = [
            'phone' => input('param.phone', 15802571830),
            'type' => input('param.type', 1)
        ];
        return call_interface('send_note',$param);
    }

    public function clear_session() {
        Session::clear();
        dump(session(''));
    }

    public function member_service_contract() {
        $id = input('param.id');
        $data = db('article')->where('id', $id)->find();
        $this->assign('data', $data);
        return view('public/member_service_contract');
    }

    public function send_msg($data) {
        $options = config('wechat');
        $wechatObj = new TPWechat($options);
        $access_token = $wechatObj->checkAuth();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
//        $data = [
//            'touser' => session('openid'),
//            'template_id' => 'rnJUnlEzUqYEEwmXLKm-U2LpMW90D-7l71DqIzp4tkY',
//            'url' => 'http://www.baidu.com',
//            'topcolor' => '#FF0000',
//            'data' => [
//                'first' => [
//                    'value' => '新订单提醒',
//                    "color" => "#173177"
//                ],
//                'keyword1' => [
//                    'value' => '新订单提醒',
//                    "color" => "#173177"
//                ],
//                'keyword2' => [
//                    'value' => date('Y-m-d H:i:s'),
//                    "color" => "#173177"
//                ],
//                'remark' => [
//                    'value' => '请尽快处理',
//                    "color" => "#173177"
//                ]
//            ]
//        ];

        post_data($url, json_encode($data)) ;
    }

    public function error_page() {
        return view('public/error');
    }

    public function subscribe() {
        return view('public/subscribe');
    }

    //上传图片
    public function get_upload_img()
    {
        $param["type"] = input('param.type');
        $files = request()->file('img');
        if ($files) {
            if (count($files) > 1) {
                foreach($files as $file){
                    $dir = $param['type'] ? $param['type'] : 'images';

                    $info = $file->validate(['size'=>5242880])->move(config('new.upload_img_save_dir') . DS . $dir);
                    if($info){
                        $img_path[] =  DS . 'uploads' . DS . $dir . DS . $info->getSaveName();
                    }else{
                        // 上传失败获取错误信息
                        return json(['code' => 0, 'msg' => $file->getError()]);
                    }
                }
            } else {
                $dir = $param['type'] ? $param['type'] : 'images';

                $info = $files->validate(['size'=>5242880])->move(config('new.upload_img_save_dir') . DS . $dir);
                if($info){
                    $img_path[] =  DS . 'uploads' . DS . $dir . DS . $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    return ['code' => 0, 'msg' => $files->getError()];
                }
            }

            if (count($img_path) == 1) {
                return json(['code' => 200, 'msg' => 'SUCCESS', 'data' => ['path' => str_replace("\\","/",$img_path[0])]]);
            } elseif (count($img_path) > 1) {
                return json(['code' => 200, 'msg' => 'SUCCESS', 'data' => ['paths' => $img_path]]);
            } else {
                return json(['code' => 400135, 'msg' => 'FAULT']);
            }
        } else {
            return json(['code' => 400135, 'msg' => 'FAULT']);
        }
        
    }

    //删除图片
    public function del_upload_img()
    {
        $tmpFile = '.'.str_replace("..","",input('param.img'));
        if(!file_exists($tmpFile)) {
            return json(["code"=>"0","msg"=>"not exists:".$tmpFile]);
        }else{
            if(!unlink($tmpFile)){
                return json(["code"=>"0","msg"=>"del error"]);
            }else{
                return json(["code"=>"200","msg"=>"ok"]);
            }
        }
    }

    // 根据经纬度逆推地址
    public function map_get_address()
    {

        $lat = input('param.lat');
        $lng = input('param.lng');
        $tmpStr = file_get_contents("http://restapi.amap.com/v3/geocode/regeo?key=23b48012ac55a9b14bc9400c94c261d9&location=".$lng.",".$lat."&poitype=&radius=1000&extensions=base&batch=false&roadlevel=0");
        echo $tmpStr;
        exit();

    }

    public function check_code() {
        if (!input('?param.phone') || !input('?param.code')) {
            return json(['code' => 0, 'msg' => '手机号码和验证码不能为空！']);
        }
        $param = [
            'phone' => input('param.phone'),
            'code' => input('param.code'),
            'type' => 1,
            'action' => 'weixin'
        ];
        $data = call_interface('check_note_code',$param);
        if ($data['code'] == 200) {
            cookie(input('param.code'), 1);
            cache('check_code_status', 1);
        }
        return json($data);
    }

    public function get_street() {
        $area = input('param.area', 0);
        $sNum = input('param.sNum', 0);
        if ($area < 1) {
            if( $sNum > 0 ){
                $area = db('region')->where('sNum', $sNum)->value("region_id");
            }
        }
        if ($area > 0) {
            $street = db('region')->where('parent_id', $area)->select();
            return json(['code' => 200, 'data' => $street]);
        } else {
            return json(['code' => 0, 'msg' => '获取失败']);
        }
    }

    // 验证安全密码
    public function check_safe_password() {
        // 解密数据
        $des = new DES3();
        $safe_password = input('param.safe_password', 0);
        $safe_password = $des->encrypt($safe_password);
        $param = [
            'safe_password' => $safe_password,
            'member_id' => session('member_id')
        ];
        $data = call_interface('check_safe_password',$param);
        return json($data);
    }
    // 验证安全密码
    public function is_set_safe_password() {
        $param = [
            'member_id' => session('member_id')
        ];
        $data = call_interface('is_set_safe_password',$param);
        return json($data);
    }

    // 加盟咨询
    public function league_consult() {
        $param = [
            'name' => input('param.name'),
            'phone' => input('param.phone'),
            'area' => input('param.area')
        ];
        $data = call_interface('league_consult',$param);
        return json($data);
    }

    // 计算提现手续费
    public function get_withdraw_poundage() {
        $param = [
            'type' => input('param.type'),
            'money' => input('param.money')
        ];
        $data = call_interface('get_withdraw_poundage',$param);
        return json($data);
    }
}
